package main

import (
	"demo/micro/protobuf/proto"
	"fmt"
	proto1 "github.com/golang/protobuf/proto"
)

func main() {
	TestPerson()
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func TestPerson() {
	var p1 proto.Person
	p1.Id = 1
	p1.Name = "林芳伊"
	p1.Gender = proto.GenderType_FEMALE
	p1.Number = "100"

	//fmt.Println(p1)

	var p2 proto.Person
	p2.Id = 2
	p2.Name = "范帅"
	p2.Gender = proto.GenderType_MALE
	p2.Number = "100"

	var c proto.ContactBook
	c.Persons = make([]*proto.Person, 2, 2)
	c.Persons[0] = &p1
	c.Persons[1] = &p2

	fmt.Println("p1 =" + c.Persons[0].String())
	fmt.Println("p2 =" + c.Persons[1].String())

	data, err := proto1.Marshal(&c)
	checkError(err)

	var c2 proto.ContactBook
	proto1.Unmarshal(data, &c2)
	fmt.Println(c2)
}
